<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户对应的角色id，如owner_id" prop="roleId">
      <el-input v-model="dataForm.roleId" placeholder="用户对应的角色id，如owner_id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="用户密码"></el-input>
    </el-form-item>
    <el-form-item label="用户角色" prop="role">
      <el-input v-model="dataForm.role" placeholder="用户角色"></el-input>
    </el-form-item>
    <el-form-item label="用户创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="用户创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          roleId: '',
          userName: '',
          password: '',
          role: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          roleId: [
            { required: true, message: '用户对应的角色id，如owner_id不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          role: [
            { required: true, message: '用户角色不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '用户创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$request({
              url: `/user/user/info/${this.dataForm.id}`,
              method: 'get',
              params: {}
            }).then((res) => {
              if (res && res.code === 200) {
                this.dataForm.roleId = res.user.roleId
                this.dataForm.userName = res.user.userName
                this.dataForm.password = res.user.password
                this.dataForm.role = res.user.role
                this.dataForm.createTime = res.user.createTime
                this.dataForm.updateTime = res.user.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$request({
              url: `/user/user/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'roleId': this.dataForm.roleId,
                'userName': this.dataForm.userName,
                'password': this.dataForm.password,
                'role': this.dataForm.role,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then((res) => {
              if (res && res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
